package com.zx.controller;

import com.zx.bean.Message;
import com.zx.mvc.ResponseBody;
import com.zx.service.AdminService;
import com.zx.util.JsonUtil;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


public class AdminController {
    @ResponseBody("/admin/login.do")

    public String login(HttpServletRequest request,HttpServletResponse response){
        //1 接参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2调用service传参数 ,并获取结果
        boolean result = AdminService.login(username, password);

        //3 根据结果 返回数据给ajax客户端
        Message msg=null;
        if (result) {
            msg=new Message(0,"登陆成功") ;
            //登陆时间更新  和ip更新
            Date date=new Date();
            String ip=request.getRemoteAddr();//拿到IP
            AdminService.updateLoginTimeAndIp(username,date,ip);
            request.getSession().setAttribute("adminUserName",username);
        }else{
            msg=new Message(-1,"登录失败" );
        }


        //将数据转化为JSON 并返回
        String json = JsonUtil.toJson(msg);

        return json;
    }
}
